package com.yuexunit.finance.member.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.FinanceFollowPersonEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
*@version 创建时间：2016年9月20日 上午10:53:52
*@author wenwenlong
*类说明
*/

@Implements(FinanceFollowPersonService.class)
public class FinanceFollowPersonServiceImpl implements FinanceFollowPersonService {

	@Override
	@Transactional
	public Long createFinanceFollowPerson(FinanceFollowPersonEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.insert(entity);
		return entity.getFollowId();
	}

	@Override
	@Transactional
	public void updateFinanceFollowPerson(FinanceFollowPersonEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public FinanceFollowPersonEntity inquireFinanceFollowPerson(Long followId) {
		return PersistUtil.get(FinanceFollowPersonEntity.class, followId);
	}

	@Override
	public List<FinanceFollowPersonEntity> inquireFinanceFollowPersonList(Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceFollowPersonEntity.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> inquireFinanceFollowEnterpriseId(Long accountId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceFollowPersonEntity.class);
		criteria.add(Restrictions.eq("accountId", accountId));
		criteria.setProjection(Projections.property("enterpriseId"));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<FinanceFollowPersonEntity> inquireFinanceFollowPersonPageList(String followName, String clientName,
			Date dateStart, Date dateEnd, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceFollowPersonEntity.class);
		if (StringUtil.isNotBlank(followName)) {
			criteria.add(Restrictions.like("followName", followName));
		}
		
		if (StringUtil.isNotBlank(clientName)) {
			criteria.add(Restrictions.like("clientName", clientName));
		}
		
		if (dateStart != null) {
			criteria.add(Restrictions.ge("dateStart", dateStart));
		}
		
		if (dateEnd != null) {
			criteria.add(Restrictions.le("dateEnd", dateEnd));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> inquireFinanceFollowByfollowName(String followName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceFollowPersonEntity.class);
		if (StringUtil.isNotBlank(followName)) {
			criteria.add(Restrictions.like("followName", "%" + followName + "%"));
		}
		criteria.setProjection(Projections.property("enterpriseId"));
		return PersistUtil.get(criteria);
	}
}
